package com.espirit.eap.sa;
import java.util.Collection;
import java.util.HashSet;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.hibernate.annotations.BatchSize;

import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.annotation.DomainModel;
import com.espirit.eap.annotation.ModelDisplayProperty;
import com.espirit.eap.manager.Department;
import com.espirit.eap.pagelayout.AvabButton;
import com.espirit.eap.relationship.Party;
import com.googlecode.cswish.security.auth.UserInfo;

@DomainModel
@Entity(name="com.espirit.eap.sa.User")
@ModelDisplayProperty("code")
@BatchSize(size=10)
public class User extends Party implements UserInfo,ICheckModelDuplicateInfo {
	
	private static final long serialVersionUID = 6941368790239123213L;

	private LoginUser loginUser;
	
	// transient
	private String password1;
	private String password2;
	private User users;
	
	private Employee employee;
	
	
	@ManyToOne(targetEntity = LoginUser.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL)
	public LoginUser getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(LoginUser loginUser) {
		this.loginUser = loginUser;
	}

	
	@Transient
	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}
	
	@Transient
	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}
	
	public void setUsers(User user){
		this.users = user;
	}
	@Transient
	public User getUsers(){
		return users;
	}
	
	public boolean permit(com.espirit.eap.pagelayout.Model model,AvabButton ab){
		return permit(model,ab.getName());
	}
	public boolean permit(com.espirit.eap.pagelayout.Model model,String buttonName){
		return false;
	}
	

	@Transient
	@Override
	public String getLoginId() {
		return String.valueOf(getId());
	}
	
	@Transient
	@Override
	public String[] getRoleIds() {
		return null;
	}

	private final static String[] gatherProperties = new String[]{"code"};
	@Override
	public String[] gatherProperties() {
		return gatherProperties;
	}
	
	@Transient
	public Collection<Integer> getAllRelatedUsers(){
		Collection<Integer> ids = new HashSet<Integer>();
		for(Department department : getDepartments()){
			ids.addAll(department.getAllUserIds());
		}
		return ids;
	}

	@ManyToOne
	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

}